<?php

declare(strict_types=1);
/**
 * 
 */
namespace WanZ\App\User\Logic\Traits;

use Hyperf\Context\Context;
use Hyperf\Utils\ApplicationContext;
use WanZ\App\User\Consumer\Company;
use MoChat\Framework\Constants\ErrorCode;
use MoChat\Framework\Exception\CommonException;

trait CompanyTrait
{
    private $dbNamePrefix = 'wz_all_database_name_';
    private $cacheKey = 'cache:wzCompanyPayConfig_';

    private $defaultExpireTime = 1800;  //30分钟
    
    /**
     * 获取指定公司的支付配置信息
     *
     * @param $companyId
     * @return array
     * @throws ApiException
     * @throws GuzzleException
     */
    public function getCompanyPayConfigInfoByCompanyId($companyId): array
    {
        return $this->getCompanyConfigInfoByCompanyIdAndPlatform((string)$companyId)['payConfig'] ?? [];
    }
        
    /**
     * 获取指定公司的基础信息
     *
     * @param $companyId
     * @return array|mixed
     * @throws ApiException
     * @throws GuzzleException
     */
    public function getCompanyBaseInfoByCompanyId($companyId): array
    {
        return $this->getCompanyConfigInfoByCompanyIdAndPlatform((string)$companyId)['baseInfo'] ?? [];
    }
        
    /**
     * 获取公司配置相关信息
     *
     * @param string $key
     * @return array
     * @throws ApiException
     * @throws GuzzleException
     */
    private function getCompanyConfigInfoByCompanyIdAndPlatform(string $key): array
    {
        $type   = $this->getPlatformType();
        $source = Context::get('minSource', '');
        if (empty($source)) {
            $source = Context::get('mini_source', 'wz');
        }

        $cacheKey = $this->cacheKey.$type.'_'.$key.'_'.$source;
        $res = $this->getRedis()->hGetAll($cacheKey);

        if (isset($res['value']) && !empty($res['value'])) {
            return json_decode($res['value'], true);
        }

        try {
            $companyConfigInfo = make(Company::class)->_(['company_id' => $key, 'type' => $type])->getCompanyConfigByCompanyId();
            $this->setData($cacheKey, $companyConfigInfo, $this->defaultExpireTime);

            return $companyConfigInfo;
        } catch (\Exception $exception) {
             throw new CommonException($exception->getCode(), $exception->getMessage());
        }
    } 

    /**
     * 设置redis缓存数据
     *
     * @param string $key
     * @param array $data
     * @param int $expire
     * @return void
     */
    private function setData(string $key, array $data, int $expire = 1800): void
    {
        $this->getRedis()->hMSet($key, ['value' => json_encode($data, 320)]);
        $this->getRedis()->expire($key, $expire);
    }

    /**
     * 获取redis实例
     * @return Pool
     */
    private function getRedis(): \Hyperf\Redis\Redis
    {
        $container = ApplicationContext::getContainer();
        return $container->get(\Hyperf\Redis\Redis::class);
    }    

    /**
     * 获取平台类型
     * @return mixed
     */
    private function getPlatformType(): ?string
    {
        return Context::get('platformType', 'default');
    }
}
